/**
 * Created by ROGSTAR on 2016/11/29.
 */
require.config(window.jshead);
require(['jquery', 'pages','navigator'], function ($,pg) {

        var oDiv = document.getElementById('div1');
        var oNowTime = document.getElementById('nowTime');
        var aTd = oDiv.getElementsByTagName('td');
        var aNowSpan = oNowTime.getElementsByTagName('span');
        var oDate = new Date();
        var bottomTable=document.getElementById('bottomTable');
        var detailItem=document.getElementById('detailItem');

        var clickDateArr = [];

        showDate(oNowTime,oDate.getFullYear(),oDate.getMonth()+1);
        hideLastTr();
        showBtn();

        function showDate(obj,year,month,bBtn){
            var oDate = new Date();
            var dayNum = 0;
            var lastMonthdayNum=0;
            if(!obj.bBtn){
                obj.oTitle = document.createElement('div');
                obj.oTitle.className = 'title';
                obj.appendChild(obj.oTitle);

                var oTable = document.createElement('table');
                var oThead = document.createElement('tHead');
                var oTr = document.createElement('tr');
                var arr = ['一','二','三','四','五','六','日'];
                for(var i=0;i<7;i++){
                    var oTh = document.createElement('th');
                    oTh.innerHTML = arr[i];
                    oTr.appendChild(oTh);
                }
                oThead.appendChild(oTr);
                oTable.appendChild(oThead);

                var oTbody = document.createElement('tBody');
                for(var i=0;i<6;i++){
                    var oTr = document.createElement('tr');
                    for(var j=0;j<7;j++){
                        var oTd = document.createElement('td');
                        oTr.appendChild(oTd);
                    }
                    oTbody.appendChild(oTr);
                }
                oTable.appendChild(oTbody);
                obj.appendChild(oTable);
                obj.bBtn = true;
            }

            obj.oTitle.innerHTML = ( '<div><span class="leftbutton"></span><span>'+year+'</span>年<span>'+month+'</span>月<span class="rightbutton"></span></div>');

            var aTd = obj.getElementsByTagName('td');
            for(var i=0;i<aTd.length;i++){
                aTd[i].innerHTML = '';
            }

            if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
                dayNum = 31;
            }
            else if(month==4 || month==6 || month==9 || month==11){
                dayNum = 30;
            }
            else if(month==2 && isLeapYear(year)){
                dayNum = 29;
            }
            else{
                dayNum = 28;
            }

            if( month==5 || month==7 || month==10 || month==12){
                lastMonthdayNum = 30;
            }
            else if(month==2 ||month==1 || month==4 || month==6 ||month==8 || month==9 || month==11){
                lastMonthdayNum = 31;
            }
            else if(month==3 && isLeapYear(year)){
                lastMonthdayNum = 29;
            }
            else{
                lastMonthdayNum = 28;
            }

            oDate.setFullYear(year);
            oDate.setMonth(month-1);
            oDate.setDate(1);

            switch(oDate.getDay()){
                case 0:
                    for(var i=0;i<dayNum;i++){
                        aTd[i+6].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    var leftdayNum0=lastMonthdayNum-5;
                    for(var j=0;j<6;j++){
                        aTd[j].innerHTML=leftdayNum0++;
//                        aTd[j].style.color='#97929c';
                    }

                    break;
                case 1:
                    for(var i=0;i<dayNum;i++){
                        aTd[i].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    break;
                case 2:
                    for(var i=0;i<dayNum;i++){
                        aTd[i+1].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    aTd[0].innerHTML=lastMonthdayNum;
//                    aTd[0].style.color='#97929c';

                    break;
                case 3:
                    for(var i=0;i<dayNum;i++){
                        aTd[i+2].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    aTd[0].innerHTML=lastMonthdayNum-1;
                    aTd[1].innerHTML=lastMonthdayNum;
//                    aTd[0].style.color=aTd[1].style.color='#97929c';
                    break;
                case 4:
                    for(var i=0;i<dayNum;i++){
                        aTd[i+3].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    aTd[0].innerHTML=lastMonthdayNum-2;
                    aTd[1].innerHTML=lastMonthdayNum-1;
                    aTd[2].innerHTML=lastMonthdayNum;
//                    aTd[0].style.color=aTd[1].style.color=aTd[2].style.color='#97929c';
                    break;
                case 5:
                    for(var i=0;i<dayNum;i++){
                        aTd[i+4].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    var leftdayNum5=lastMonthdayNum-3;
                    for(var j=0;j<4;j++){
                        aTd[j].innerHTML=leftdayNum5++;
//                        aTd[j].style.color='#97929c';
                    }
                    break;
                case 6:
                    for(var i=0;i<dayNum;i++){
                        aTd[i+5].innerHTML = i+1;
                    }
                    for(var k=0;k<6;k++){
                        aTd[k].style.color='';
                    }
                    var leftdayNum6=lastMonthdayNum-4;
                    for(var j=0;j<5;j++){
                        aTd[j].innerHTML=leftdayNum6++;
//                        aTd[j].style.color='#97929c';
                    }
                    break;

            }

            var dateString= year +'-'+ month;
            getShowDate(dateString)
        }

        function getShowDate(date){
            $.ajax({
                type:'POST',
                url:"/user/calendarDisplayList",
                dataType:'json',
                data:{date:date},
                success: function (data) {
                    var getData=data;
                    var investListDate=[];
                    var repayListDate=[];
                    var repayList1=[];
                    var repayList2=[];

                    for(var i=0;i<getData.investList.length;i++){
                        investListDate.push(getData.investList[i].addtime);
                    }
                    for(var j=0;j<getData.repayList.length;j++){
                        if(getData.repayList[j].collectStatus==1){
                            repayList1.push(getData.repayList[j].actualCollectTime)
                        }
                        else{
                            repayList2.push(getData.repayList[j].collectTime)
                        }

                        repayListDate = repayList1.concat(repayList2);

                    }



                    var newd=[];
                    var newE=[];

                    for(var k=0;k<investListDate.length;k++){
                        var d=new Date();
                        d.setTime(investListDate[k]);
                        newd.push(d);
                        showCircle(d,'invest')
                    }


                    for(var l=0;l<repayListDate.length;l++){
                        var e=new Date();
                        e.setTime(repayListDate[l]);
                        newE.push(e);
                        showCircle(e,'money')
                    }


                    for(var dd=0;dd<newd.length;dd++){
                        for(var ee=0;ee<newE.length;ee++){
                            if(newd[dd].getFullYear()==newE[ee].getFullYear() && newd[dd].getMonth()==newE[ee].getMonth() && newd[dd].getDate()==newE[ee].getDate()){
                                showCircle(newd[dd],'money-invest')

                            }

                        }
                    }


                    var newtd = document.getElementsByTagName('td');
                    for(var i=0;i<newtd.length;i++){
                        newtd[i].onclick=null;
                    }


                    getShowList();
                },
                error: function (data) {

                }

            });
        }

        function getShowList(){
            clickDateArr=[];
            for(var ii=0;ii<aTd.length;ii++){
                if(aTd[ii].className.indexOf('invest')!=-1 || aTd[ii].className.indexOf('money')!=-1 || aTd[ii].className.indexOf('money-invest')!=-1){
                    clickDateArr.push(aTd[ii])
                }

            }
            for(var jj=0;jj<clickDateArr.length;jj++){

                clickDateArr[jj].onclick= function () {

                    for(var jjj=0;jjj<aTd.length;jjj++){
                        removeClass(aTd[jjj],'today');
                    }

                    addClass(this, 'today');
                    detailItem.innerHTML=""+aNowSpan[1].innerHTML+"年"+aNowSpan[2].innerHTML+"月"+this.innerHTML+"日的项目";
                    var viewDate=aNowSpan[1].innerHTML+'-'+aNowSpan[2].innerHTML+'-'+ this.innerHTML;


                    pg.paging(1, 6, "/user/calendarList", 'bottomTable', 'bottomTableTemp',{viewDate:viewDate} , '', '');

                }
            }

            if(clickDateArr.length>0){
                for(var i=0;i<clickDateArr.length;i++){
                    if(clickDateArr[i].innerHTML==oDate.getDate() && aNowSpan[1].innerHTML == oDate.getFullYear() && aNowSpan[2].innerHTML == oDate.getMonth()+1){
                        clickDateArr[i].click()
                    }
                }
            }

            showCircle(oDate,'today');


        }

        function hasClass(obj, cls) {
            return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
        }

        function addClass(obj, cls) {
            if (!hasClass(obj, cls))
                obj.className += " " + cls;
        }

        function removeClass(obj, cls) {
            if (hasClass(obj, cls)) {
                var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
                obj.className = obj.className.replace(reg, ' ');
            }
        }

        function showCircle(date,className){
            var result = [];

            var index=0;
            for(var i=0;i<aTd.length;i++){
                if(aTd[i].innerHTML==1){
                    index=i;
                }
            }

            for(var j=index;j<aTd.length-index;j++)
                if(aTd[j].innerHTML!=''){
                    result.push(aTd[j]);
                }

            if(aNowSpan[1].innerHTML == date.getFullYear() && aNowSpan[2].innerHTML == date.getMonth()+1 ) {
                for (var k = 0; k < result.length; k++) {
                    if ( date.getDate() == result[k].innerHTML) {
                        addClass(result[k],className)


                    }
                }
            }

        }

        function isLeapYear(year){
            if(year%4==0 && year%100!=0){
                return true;
            }
            else{
                if(year%400==0){
                    return true;
                }
                else{
                    return false;
                }
            }
        }

        function showBtn(){
            detailItem.innerHTML='';
            bottomTable.innerHTML='';
            var Month = parseInt(aNowSpan[2].innerHTML);
            var Year = parseInt(aNowSpan[1].innerHTML);


            aNowSpan[0].onclick = function(){

                if(Month==10 && Year==2015){
                    this.onclick=null;
                    return
                }


                for(var i=0;i<aTd.length;i++){
                    aTd[i].className='';

                }

                if(Month == 1){
                    showDate(oNowTime,Year-1,12);
                }
                else{
                    showDate(oNowTime,Year,Month-1);
                }



                showBtn();
                hideLastTr();
                //getShowDate();
            };

            aNowSpan[3].onclick = function(){


                for(var i=0;i<aTd.length;i++){
                    aTd[i].className='';

                }
                if(Month == 12){
                    showDate(oNowTime,Year+1,1);
                }
                else{
                    showDate(oNowTime,Year,Month+1);
                }

                showBtn();
                hideLastTr();
                //getShowDate();
            };

        }

        function hideLastTr(){
            var bBtn = true;

            for(var i=35;i<42;i++){
                if(aTd[i].innerHTML!=''){
                    bBtn = false;
                }
            }

            if(bBtn){
                for(var i=35;i<42;i++){
                    aTd[i].parentNode.style.display = 'none';
                }
            }
            else{
                for(var i=35;i<42;i++){
                    aTd[i].parentNode.style.display = '';
                }
            }

        }

});